const Service = require('egg').Service;

class MatchService extends Service {

    //随机返回指定数量的题目,题目由标题和答案组成
    //所以这里构建一个vo对象,用来结合标题和答案
    async random(size) {
        let matchItemsList = await this.ctx.app.mysql.query(`SELECT * FROM match_items ORDER BY RAND() LIMIT ${size};`);
        let itemId = matchItemsList.map(element => {
            return element.id;
        });
        let options = await this.ctx.app.mysql.select("match_option",
            {
                where: { match_id: itemId },
            });
        let res = [];
        matchItemsList.forEach(e => {
            let obj = e;
            obj.solutions = [];
            options.forEach(ee => {
                if (ee.match_id === e.id) {
                    obj.solutions.push(String.fromCharCode(65 + ee.sort) + ". " + ee.title)
                }
            })
            res.push(obj)
        });
        return res
    }
}
module.exports = MatchService;